package com.wd.timer;

import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.wd.dao.AlexaDAOImpl;
import com.wd.http.HttpRequest;
import com.wd.model.AlexaDataReference;
import com.wd.parser.ParserHtml;
import com.wd.shared.Configuration;
import com.wd.shared.Sleeper;
import com.wd.util.DateUtil;
import com.wd.util.StringUtil;

public class DoTask {

	private static Log log = LogFactory.getLog(DoTask.class);

	private static AlexaDAOImpl impl = new AlexaDAOImpl();

	private static HttpRequest httpResult = new HttpRequest();

	public DoTask() {
	}

	public void doWork() {
		Map<Integer, String> map = impl.getEntIdMap();
		// Map<Integer, String> map = new HashMap<Integer, String>();
		// map.put(1, "http://guosen.com.cn,http://aaaaa.com");
		// map.put(2, "http://www.newone.com.cn");
		// map.put(3, "www.qq.com");
		log.info("domain size -->" + map.size());
		if (!map.isEmpty()) {
			Set<Entry<Integer, String>> entrySet = map.entrySet();
			int i = 1;
			for (Entry<Integer, String> entry : entrySet) {
				AlexaDataReference model = getAlexaDataReference(
						entry.getKey(), entry.getValue());
				if (model == null) {
					continue;
				}
				impl.insertAlexaDataReference(model);
				log.info("done size --> "+i);
				i++;
				Sleeper.sleep(8);
			}
		}
	}

	public AlexaDataReference getAlexaDataReference(int entId, String domain) {
		AlexaDataReference model = null;
		if (!StringUtil.isValidString(domain)) {
			return model;
		}
		String fullUrl = Configuration.getInstance().getAlexaUrl();
		if (domain.indexOf(",") != -1) {
			domain = domain.split(",")[0];
		}
		if (domain.indexOf("www.") != -1) {
			domain = domain.substring(domain.indexOf("www.") + 4, domain
					.length());
		} else if (domain.indexOf("http://") != -1) {
			domain = domain.substring(7, domain.length());
		}
		fullUrl += domain;
		log.info("url -->" + fullUrl);

		String page = httpResult.getRequest(fullUrl);
		page = StringUtil.filterLn(page);
		if (!StringUtil.isValidString(page)) {
			return model;
		}
		model = new AlexaDataReference();
		model.setEnt_id(entId);
		String traffickRank = ParserHtml.getTrafficRankFromPage(page);
		String trafficRankCn = ParserHtml.getTrafficRankCnFromPage(page);
		String siteLinkingIn = ParserHtml.getSiteLinkingInFromPage(page);
		String avgLoadTime = ParserHtml.getAvgLoadTimeFromPage(page);
		String pageviewsGlobalPercent = ParserHtml
				.getPageviewsGlobalPercentFromPage(page);
		String pageviewsPerUser = ParserHtml.getPageviewsPerUserFromPage(page);
		String seRefererPercentage = ParserHtml
				.getseRefererPercentagePage(page);
		String subDomainSpread = ParserHtml.getSubDomainSpreadFromPage(page,
				domain);
		String threeMonthGrouthMovement = ParserHtml
				.get3rdMGrowthMovement(page);

		if (traffickRank != null && !"".equals(traffickRank)) {
			model.setTraffic_rank(Integer.parseInt(traffickRank
					.replace(",", "")));
		}
		if (threeMonthGrouthMovement != null
				&& !"".equals(threeMonthGrouthMovement)) {
			model.setThree_m_growth_movement(Integer
					.parseInt(threeMonthGrouthMovement));
		}
		if (trafficRankCn != null && !"".equals(trafficRankCn)) {
			model.setTraffic_rank_cn(Integer.parseInt(trafficRankCn.replace(
					",", "")));
		}
		if (siteLinkingIn != null && !"".equals(siteLinkingIn)) {
			model.setSite_linking_in(Integer.parseInt(siteLinkingIn.replace(
					",", "")));
		}
		if (avgLoadTime != null && !"".equals(avgLoadTime)) {
			model.setAvg_load_time(Float.parseFloat(ParserHtml
					.getAvgLoadTimeFromPage(page)));
		}
		if (pageviewsGlobalPercent != null
				&& !"".equals(pageviewsGlobalPercent)) {
			model.setPageviews_global_percent(Float
					.parseFloat(pageviewsGlobalPercent));
		}
		if (pageviewsPerUser != null && !"".equals(pageviewsPerUser)) {
			model.setPageviews_per_user(Float.parseFloat(pageviewsPerUser));
		}
		if (seRefererPercentage != null && !"".equals(seRefererPercentage)) {
			model.setSe_referer_percentage(Float
					.parseFloat(seRefererPercentage));
		}
		if (subDomainSpread != null && !"".equals(subDomainSpread)) {
			model.setSub_domain_spread(subDomainSpread);
		}
		model.setInput_date(DateUtil.getCurrentFullDateString());
		return model;
	}
}
